package com.johnny.xia.util.elk;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.text.csv.CsvReader;
import cn.hutool.core.text.csv.CsvUtil;
import cn.hutool.core.text.csv.CsvWriter;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName : LogProcess
 * @Description :
 * @Author : Xia Long
 * @MailTo : johnny.xia@qq.com
 * @Date: 2024/10/14 10:54
 */
public class LogProcess {

    public static void main(String[] args) throws FileNotFoundException {

        String csvFile ="/Users/johnny/Desktop/Moncle活动相关/数据对比/usersource.csv";

        // 指定 CSV 文件输出路径
        File wfile = FileUtil.file("/Users/johnny/Desktop/Moncle活动相关/数据对比/usersource_after.csv");


        // 创建 CsvWriter
        CsvWriter writer = CsvUtil.getWriter(wfile, Charset.forName("UTF-8"));
        // 写入表头
        writer.write(new String[]{"accountNo", "mobile","sources","elk"});


        // 获取 CSV 文件流
        InputStream inputStream = ResourceUtil.getStream(csvFile);

        // 使用 CsvReader 读取 CSV 文件
        CsvReader reader = CsvUtil.getReader();

        // 将 CSV 文件中的数据自动转换为 Java 对象列表
        List<Usersources> personList = reader.read(new FileReader(csvFile), Usersources.class);
        int n = 0;

//        System.out.println(personList.size());

        for(Usersources user : personList){
            user.setN(++n);
            String cookie = "_ga_MTDXSPX2ND=GS1.1.1727684155.1.0.1727684155.0.0.0; _ga=GA1.1.1027461548.1727684156; nginxauth=eHNoMTE3ODA6NTM1NDIzMTZAeGxsLi4=; UNEX-SESSION-UAT=bdce0d6f-3f17-4a25-bb5d-1de96a74c1ee; UNEX-SESSION-PROD=507aa9cc-8026-4e10-b069-1980c3551e2c; INGRESSCOOKIE=1728871984.427.180950.237306|2f3eae6adea9c0c3eb116535438cfe4b";
            String userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36";

            String body = "{\"version\":true,\"size\":2000,\"sort\":[{\"@timestamp\":{\"order\":\"desc\",\"unmapped_type\":\"boolean\"}}],\"aggs\":{\"2\":{\"date_histogram\":{\"field\":\"@timestamp\",\"fixed_interval\":\"3h\",\"time_zone\":\"Asia/Shanghai\",\"min_doc_count\":1}}},\"stored_fields\":[\"*\"],\"script_fields\":{},\"docvalue_fields\":[{\"field\":\"@timestamp\",\"format\":\"date_time\"}],\"_source\":{\"excludes\":[]},\"query\":{\"bool\":{\"must\":[{\"query_string\":{\"query\":\"\\\"https://crmcustomer.moncler.com.cn/api/Integration/UpsertCustomer\\\" AND \\\"request\\\" AND \\\"+86"+ user.getMobile() +"\\\"\",\"analyze_wildcard\":true,\"time_zone\":\"Asia/Shanghai\"}}],\"filter\":[{\"range\":{\"@timestamp\":{\"gte\":\"2024-10-06T02:51:53.514Z\",\"lte\":\"2024-10-14T02:51:53.514Z\",\"format\":\"strict_date_optional_time\"}}}],\"should\":[],\"must_not\":[]}},\"highlight\":{\"pre_tags\":[\"@kibana-highlighted-field@\"],\"post_tags\":[\"@/kibana-highlighted-field@\"],\"fields\":{\"*\":{}},\"fragment_size\":2147483647}}";

            String bodyStr = HttpRequest.post("https://elk.baozun.com/elasticsearch/moncler-applog*/_search?rest_total_hits_as_int=true&ignore_unavailable=true&ignore_throttled=true&preference=1728874118909")
                    .header("Cookie",cookie)
                    .header("Content-Type","application/json")
                    .header("User-Agent",userAgent)
                    .header("kbn-version","7.6.2")
                    .header("Referer","https://elk.baozun.com/app/kibana")
                    .header("Cache-Control","no-cache")

                    .body(body)
                    .execute().body();

            List<Integer> logs = process(bodyStr);
            String _elk = "";
            for(Integer log : logs){
                _elk+=log+"|";
            }
            if(logs != null){
                user.setElk(_elk);
            }
            writer.write(new String[]{user.getAccount_no(),user.getMobile(),user.getSource(),user.getElk()});
            System.out.println(JSON.toJSONString(user));

        }

        writer.close();





    }

    private static List<Integer> process(String body){
        List<Integer> list = new ArrayList<>();
//        body  = "{\"took\":35,\"timed_out\":false,\"_shards\":{\"total\":20,\"successful\":20,\"skipped\":0,\"failed\":0},\"hits\":{\"total\":1,\"max_score\":null,\"hits\":[{\"_index\":\"moncler-applog-allapp-prod-000276\",\"_type\":\"_doc\",\"_id\":\"fimXd5IBiJOBuE0EnWEF\",\"_version\":1,\"_score\":null,\"_source\":{\"log\":{\"file\":{\"path\":\"/service/logs/app/prod/moncler/moncler-store/10.245.255.118/info.log\"}},\"traceId\":\"02dc28581ee6d135\",\"app\":\"moncler-store\",\"@version\":\"1\",\"loggerFqcn\":\"org.apache.logging.log4j.spi.AbstractLogger\",\"fields\":{\"type\":\"k8s\",\"flag\":\"ack\",\"ip\":\"10.245.255.118\",\"nodeip\":\"10-41-35-92\",\"env\":\"prod\",\"class\":\"applog\",\"project_name\":\"moncler\"},\"segmentId\":\"\",\"thread\":\"moncler-store-crm-task-upload-2\",\"timestamp\":\"2024-10-11 02:01:14:251\",\"spanId\":\"0b600faf99282d1d\",\"thrown\":\"%{thrown}\",\"@timestamp\":\"2024-10-10T18:01:14.251Z\",\"message\":\"request=======>\\\"{  \\\"url\\\": \\\"https://crmcustomer.moncler.com.cn/api/Integration/UpsertCustomer\\\",  \\\"method\\\": \\\"POST\\\",  \\\"headers\\\": {},  \\\"body\\\": \\\"{\\\"SourceSystemId\\\":\\\"1843655613665288\\\",\\\"Base_Prefix\\\":\\\"MISS\\\",\\\"Base_FirstName\\\":\\\"悦溪\\\",\\\"Base_LastName\\\":\\\"黄\\\",\\\"Gender\\\":\\\"2\\\",\\\"BirthDate\\\":\\\"Jan 1, 1900 12:00:00 AM\\\",\\\"Email\\\":\\\"3537923904@qq.com\\\",\\\"MobilePhone_Number\\\":\\\"+8618253344538\\\",\\\"Address1_Line1\\\":\\\"东港区\\\",\\\"Address1_City\\\":\\\"日照市\\\",\\\"Address1_StateOrProvince\\\":\\\"山东省\\\",\\\"Address1_Country\\\":\\\"CHN\\\",\\\"Address1_Region\\\":\\\"370000\\\",\\\"CreatedOn\\\":\\\"Oct 8, 2024 10:12:06 PM\\\",\\\"ModifiedOn\\\":\\\"Oct 8, 2024 10:12:32 PM\\\",\\\"CreatedStore\\\":\\\"Y516\\\",\\\"LastLeadSource\\\":107,\\\"NewsLetterPreferenceMan\\\":false,\\\"NewsLetterPreferenceWoman\\\":false,\\\"NewsLetterPreferenceKid\\\":false,\\\"NewsLetterPreferenceGrenoble\\\":false,\\\"NewsLetterPreferenceGenius\\\":false,\\\"Privacy\\\":true,\\\"Profiling\\\":true,\\\"Marketing\\\":true,\\\"ChinaCrossBorder\\\":true,\\\"MarketingEmail\\\":true,\\\"MarketingMail\\\":true,\\\"MarketingPhoneCall\\\":true,\\\"MarketingSMS\\\":true,\\\"WechatUnionid\\\":\\\"ozgmXvwbusuMqeFqZbwH1tiPCg74\\\"}\\\"}\\\"\",\"project_name\":\"moncler\",\"source\":{\"line\":54,\"class\":\"com.baozun.tic.moncler.store.service.config.OkHttpInterceptor\",\"method\":\"intercept\",\"file\":\"OkHttpInterceptor.java\"},\"threadId\":2854,\"unique_appname\":\"moncler-moncler-store\",\"logsize_bytes\":1706,\"loggerName\":\"com.baozun.tic.moncler.store.service.config.OkHttpInterceptor\",\"threadPriority\":5,\"level\":\"INFO\"},\"fields\":{\"@timestamp\":[\"2024-10-10T18:01:14.251Z\"]},\"highlight\":{\"message\":[\"@kibana-highlighted-field@request@/kibana-highlighted-field@=======>\\\"{  \\\"url\\\": \\\"@kibana-highlighted-field@https@/kibana-highlighted-field@://@kibana-highlighted-field@crmcustomer.moncler.com.cn@/kibana-highlighted-field@/@kibana-highlighted-field@api@/kibana-highlighted-field@/@kibana-highlighted-field@Integration@/kibana-highlighted-field@/@kibana-highlighted-field@UpsertCustomer@/kibana-highlighted-field@\\\",  \\\"method\\\": \\\"POST\\\",  \\\"headers\\\": {},  \\\"body\\\": \\\"{\\\"SourceSystemId\\\":\\\"1843655613665288\\\",\\\"Base_Prefix\\\":\\\"MISS\\\",\\\"Base_FirstName\\\":\\\"悦溪\\\",\\\"Base_LastName\\\":\\\"黄\\\",\\\"Gender\\\":\\\"2\\\",\\\"BirthDate\\\":\\\"Jan 1, 1900 12:00:00 AM\\\",\\\"Email\\\":\\\"3537923904@qq.com\\\",\\\"MobilePhone_Number\\\":\\\"+@kibana-highlighted-field@8618253344538@/kibana-highlighted-field@\\\",\\\"Address1_Line1\\\":\\\"东港区\\\",\\\"Address1_City\\\":\\\"日照市\\\",\\\"Address1_StateOrProvince\\\":\\\"山东省\\\",\\\"Address1_Country\\\":\\\"CHN\\\",\\\"Address1_Region\\\":\\\"370000\\\",\\\"CreatedOn\\\":\\\"Oct 8, 2024 10:12:06 PM\\\",\\\"ModifiedOn\\\":\\\"Oct 8, 2024 10:12:32 PM\\\",\\\"CreatedStore\\\":\\\"Y516\\\",\\\"LastLeadSource\\\":107,\\\"NewsLetterPreferenceMan\\\":false,\\\"NewsLetterPreferenceWoman\\\":false,\\\"NewsLetterPreferenceKid\\\":false,\\\"NewsLetterPreferenceGrenoble\\\":false,\\\"NewsLetterPreferenceGenius\\\":false,\\\"Privacy\\\":true,\\\"Profiling\\\":true,\\\"Marketing\\\":true,\\\"ChinaCrossBorder\\\":true,\\\"MarketingEmail\\\":true,\\\"MarketingMail\\\":true,\\\"MarketingPhoneCall\\\":true,\\\"MarketingSMS\\\":true,\\\"WechatUnionid\\\":\\\"ozgmXvwbusuMqeFqZbwH1tiPCg74\\\"}\\\"}\\\"\"]},\"sort\":[1728583274251]}]},\"aggregations\":{\"2\":{\"buckets\":[{\"key_as_string\":\"2024-10-11T00:00:00.000+08:00\",\"key\":1728576000000,\"doc_count\":1}]}}}";
        JSONObject jsonObject = JSONObject.parseObject(body);
        JSONObject hits = jsonObject.getJSONObject("hits");
        JSONArray hitsArray = hits.getJSONArray("hits");
        if(hitsArray.size() > 0){
            for(int i=0;i<hitsArray.size();i++){
                try {
                    JSONObject _data = hitsArray.getJSONObject(i);
                    JSONObject _source = _data.getJSONObject("_source");
                    String message = _source.getString("message");
                    String tmp = message.split("\"body\": \"")[1];
                    tmp = tmp.substring(0, tmp.length() - 3);
                    JSONObject tmpJSON = JSONObject.parseObject(tmp);
                    list.add(Integer.parseInt(tmpJSON.getString("LastLeadSource")));
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        return list;
    }
}
